package cn.gx.learn.security_boot_jwt_websocket.aop.service;

import cn.gx.learn.security_boot_jwt_websocket.aop.dal.date.Log;
import cn.gx.learn.security_boot_jwt_websocket.aop.dal.mapper.LogMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.concurrent.ThreadPoolExecutor;
@Slf4j
@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, Log> implements LogService {

    public  void logTaskCompletion(ThreadPoolExecutor pool, long startTime, boolean success, Exception exception) {
        long runTime = System.currentTimeMillis() - startTime;
        String threadName=Thread.currentThread().getName();

        String poolStatus = String.format(
                "线程池状态 - 活跃:%d, 核心:%d, 最大:%d, 当前:%d, 已完成:%d, 队列:%d",
                pool.getActiveCount(),
                pool.getCorePoolSize(),
                pool.getMaximumPoolSize(),
                pool.getPoolSize(),
                pool.getCompletedTaskCount(),
                pool.getQueue().size()
        );

        if (success) {
            log.info("异步任务执行成功 - 线程: {}, 耗时: {}ms, {}", threadName, runTime, poolStatus);
        } else {
            log.error("异步任务执行失败 - 线程: {}, 耗时: {}ms, 错误: {}, {}",
                    threadName, runTime, exception.getMessage(), poolStatus);
        }
    }
}
